import 'package:Second/routerdemo/AppBarDemoPage.dart';
import 'package:Second/routerdemo/TabBarController.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  final Map<String, Function> routes = {
    "/": (context) => TabsLogin(),
    "/appBarDemo": (context) => AppBarDemoPage(),
    "/tabBarControllerPage": (context) => TabBarControllerPage(),
  };
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // home: TabsLogin(),
      initialRoute: '/',
      onGenerateRoute: (RouteSettings settings) {
        final String? name = settings.name;
        final Function? pageContentBuilder = this.routes[name];

        if (pageContentBuilder != null) {
          if (settings.arguments != null) {
            final Route route = MaterialPageRoute(
                builder: (context) =>
                    pageContentBuilder(context, arguments: settings.arguments));
            return route;
          } else {
            final Route route = MaterialPageRoute(
                builder: (context) => pageContentBuilder(context));
            return route;
          }
        }
      },
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

class TabsLogin extends StatefulWidget {
  final int index;
  TabsLogin({Key? key, this.index = 1}) : super(key: key);

  @override
  _TabsState createState() => _TabsState(index);
}

class _TabsState extends State<TabsLogin> {
  int _currentIndex = 0;
  List pageList = [Login(), HomePage()];

  _TabsState(index) {
    this._currentIndex = index;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Web'),
      ),
      body: pageList[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "Login"),
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页")
        ],
        currentIndex: this._currentIndex,
        onTap: (index) {
          setState(() {
            this._currentIndex = index;
          });
        },
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          //TabBar给title和给bottom界面显示位置不一样
          title: TabBar(
            tabs: [
              Tab(
                text: "热销",
              ),
              Tab(text: "热销")
            ],
          ),
        ),
        body: TabBarView(
          children: [
            Column(children: [
              ElevatedButton(
                  onPressed: () {
                    Navigator.pushNamed(context, "/tabBarControllerPage");
                  },
                  child: Text('tabBarControllerPage')),
            ]),
            ListView(children: [
              ListTile(
                title: Text("销量数据"),
              ),
              ListTile(title: Text("销量数据2"))
            ])
          ],
        ),
      ),
    );
  }
}

class Login extends StatefulWidget {
  Login({Key? key}) : super(key: key);

  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Login Page'),
      ),
      body: Center(
        child: Column(
          children: [
            Text('登录页面，输入用户名密码，点击确定'),
            ElevatedButton(onPressed: () {}, child: Text('登录'))
          ],
        ),
      ),
    );
  }
}
